import addSelButton from "@/components/diy-form/add-sel-button.vue";
import addSelBg from "@/components/diy-form/add-sel-bg.vue";
import selForm from "@/components/diy-form/sel-form.vue";
import showForm from "@/components/diy-form/show-form.vue";
import MxDatePicker from "@/components/mx-datepicker/mx-datepicker.vue";
export default {
	components:{
		addSelBg,
		addSelButton,
		selForm,
		showForm,
		MxDatePicker
	},
	data() {
		return {
			isCopy:false, //是不是复制
			info:{},
			s_id:0,
			loginStatus:false,
			userInfo:{},
			mobile:'',
			title:'',
			formData:[],
			//日期时间选择数据
			mxDatePicker:{
				showPicker: false,
				type: '',
				start_time: '',
				end_time:'',
				dk_start_time:'00:00',
				dk_end_time:'23:59',
				tx_time:'08:00',
				key:'',
			},
			setObj:{
				tx_tip:'在设定的提醒日期和时间，向已订阅用户推送打卡提醒。提醒成功率较低时系统将自动关闭提醒；如设置打卡类型为“自定义”或者“每周一次”则提醒日期为选择的打卡日期',
				tx_time_type_tip:'假设某打卡活动时间为一周，在设定的打卡日期内。例如：设置每日6点提醒。用户今日参加打卡订阅提醒后，明日如6点前未打卡，将会收到提醒。',
				tx_time:'在当日设定的时间段范围内才可以打卡',
				allow_bk_tip:'例如：用户忘记昨日打卡，则可选择日期，进行补卡操作',
				input_last_tip:'开启后，将自动填写用户最后一次参与该打卡时输入的内容',
				s_type:{
					arr:[
							{id:1,title:'每天'},
							{id:2,title:'自定义'},
							{id:3,title:'每周一次'}
						],
					index:0
				},
				weekdata:{
					titleIndex:0,
					titleArr:['提醒日期','打卡日期','可打卡日期'],
					arr:[
						{id:1,title:'星期一'},
						{id:2,title:'星期二'},
						{id:3,title:'星期三'},
						{id:4,title:'星期四'},
						{id:5,title:'星期五'},
						{id:6,title:'星期六'},
						{id:0,title:'星期日'}
						/* ['星期一','星期二','星期三','星期四','星期五','星期六','星期日'] */
					],
					index:[]
				}
			},
			txStatus:-1,
			canBk:1,//允许补卡
			inputLast:1,//自动填充上次内容
			againMore:-1,
			showPushCircletip:0,
			pushCircleStatus:-1,
			pushCircleList:[],
			push_circle_index:0,
			joinRuleList:[],
			joinRuleListIndex:0,
			resetCheckbox:true,
			loginCallBackType:'',
		}
	},
	onLoad(params){
		this.joinRuleList = this.$api.joinRuleList();
		if(params.copy && params.copy == 1)
		{
			this.isCopy = true;
		}
		if(params.s_id)
		{
			this.s_id = params.s_id;
			this.getEditInfo();
		}
		//初始化时间
		var initTime = this.$api.getDateRange('',30,5);
		this.mxDatePicker.start_time = initTime[0]?initTime[0]:'';
		this.mxDatePicker.end_time = initTime[1]?initTime[1]:'';
	},
	onShow(){
		this.onShowInit();
		if(this.$refs.show_form)
		{
			this.$refs.show_form.parentSetUsername();
		}
	},
	methods: {
		wechatRegister(e){
			if(e.code == 1)
			{
				var that = this;
				that.onShowInit();
				switch(that.loginCallBackType)
				{
					case 'pushCircleChange':
						that.pushCircleChange();
						break;
					case 'submit':
						that.submit();
						break;
				}
				that.loginCallBackType = '';
			}
		},
		onShowInit(){
			//检测是不是登录了
			this.loginStatus = this.$api.checkLogin(false);
			if(!this.userInfo.avatar)
			{
				this.userInfo = this.$api.checkUserInfo();
			}
		},
		getEditInfo(){
			var that = this;
			if(that.s_id > 0)
			{
				that.$http.post('statistics/getEditInfo',{
					s_id:that.s_id,
					is_copy:(that.isCopy == true)?1:-1
				}).then(res=>{
					if(res.code == 1)
					{
						var info = res.data.info;
						that.info = info;
						if(info.bgPath)
						{
							that.$refs.add_sel_bg.initBgFunc(info.bgPath);
						}
						if(info.join_datalist)
						{
							that.formData = JSON.parse(info.join_datalist)
						}
						if(info.content_list)
						{
							let join_contentlist = JSON.parse(info.content_list);
							that.$refs.add_sel_button.initDataList(join_contentlist)
						}
						that.title = info.title;
						that.againMore = info.again_more;
						var s_type_info = info.s_type_info?info.s_type_info:'';
						if(s_type_info)
						{
							that.txStatus =  s_type_info.tx_status;
							that.canBk =  s_type_info.can_bk;
							that.inputLast =  s_type_info.input_last;
							that.mxDatePicker.dk_start_time = s_type_info.dk_start_time;
							that.mxDatePicker.dk_end_time = s_type_info.dk_end_time;
							that.mxDatePicker.tx_time = s_type_info.tx_time;
							if(s_type_info.weeklist != -1)
							{
								that.setObj.weekdata.index = s_type_info.weeklist.split(',');
							}
							that.setObj.s_type.arr.forEach((item,index)=>{
								if(item.id == info.s_type_info.s_type_id)
								{
									that.setObj.s_type.index = index;
									that.setObj.weekdata.titleIndex = index;
								}
							})
						}
						
						if(that.isCopy == false)
						{
							that.mobile = info.mobile;
							that.mxDatePicker.start_time = info.start_time_text;
							that.mxDatePicker.end_time = info.end_time_text;
							that.pushCircleStatus = info.push_circle_status;
							if(that.pushCircleStatus == 1)
							{
								that.getCircleList();
							}
							that.userInfo.nickname = info.nickname;
							that.userInfo.avatar = info.avatar;
						}else{
							that.initCopy();
							
						}
						that.joinRuleList.forEach((item,index)=>{
							if(item.id == info.show_rule_status)
							{
								that.joinRuleListIndex = index;
							}
						})
						
					}else{
						that.$api.msg(res.msg);
					}
				})
			}
		},
		initCopy(){
			this.mobile = '';
			this.title = (this.title.indexOf('-复制') == -1)?this.title+'-复制':this.title;
			this.info.push_circle_id = 0;
		},
		
		pushCircleChange(){
			var that = this;
			if(that.isCopy == false)
			{
				uni.showModal({
					title:'友情提示',
					content:'发布后的圈子不能再次修改!',
					showCancel:false
				})
				return ;
			}else{
				var checkLogin = that.$api.checkLogin(false);
				if(checkLogin == false)
				{
					that.pushCircleStatus = -1;
					that.loginCallBackType = 'pushCircleChange';
					that.$refs.wechat_login.saveShowStatus(true);
					return ;
				}
				var status = (this.pushCircleStatus == -1)?1:-1;
				if(status == 1 && that.showPushCircletip < 1){
					uni.showModal({
						title:'友情提示',
						content:'只能选择一个圈子，选择圈子发布后不能再次修改!',
						showCancel:false
					})
					that.showPushCircletip++;
				}
				if(status == 1 && this.pushCircleList.length <1)
				{
					this.getCircleList();
				}
				this.pushCircleStatus =status; 
			}
		},
		getCircleList(){
			var that = this;
			that.$http.post('config/getCreateCircleList',{
				circle_id:that.info.push_circle_id
			}).then(res=>{
				that.push_circle_index = 0;
				if(res.code == 1)
				{
					that.pushCircleList = res.data.list;
				}else{
					that.pushCircleList = [];
					that.$api.msg(res.msg);
				}
			});
		},
		bindPickerChange(e){
			this.push_circle_index = e.detail.value;
		},
		joinRuleChange(e){
			this.joinRuleListIndex = e.detail.value;
			console.log(this.joinRuleListIndex)
		},
		selWeek(e){
			this.setObj.weekdata.index = e.detail.value;
		},
		switchChange(e){
			var key = e.target.dataset.key;
			var status = e.detail.value;
			this[key] = (status == true)?1:-1;
		},
		sTypeIndexChange(e){
			var that = this;
			var value = e.detail.value;
			this.setObj.s_type.index = value;
			this.setObj.weekdata.titleIndex = value
			this.setObj.weekdata.index = [];
			this.resetCheckbox = false;
			setTimeout(function(){
				that.resetCheckbox = true;
			},10)
			this.$forceUpdate()
			console.log(this.setObj.weekdata.index)
		},
		showTips(key){
			var tip = this.setObj[key];
			this.$api.showModal('',tip,'',{hideCancel:true});
		},
		DatePicker(type,key){//显示
			this.mxDatePicker.key = key;
			this.mxDatePicker.type = type; 
			this.mxDatePicker.showPicker = true;
			var time = this.mxDatePicker[key];
			this.mxDatePicker.value = time;
			//this.mxDatePicker.value  = '';
			//this.$forceUpdate();
		},
		DatePickerEnd(e) {//选择
			this.mxDatePicker.showPicker = false;
			if(e) {
				this.mxDatePicker[this.mxDatePicker.key] = e.value;
			}
		},
		previewForm(){
			var bgPath = this.$refs.add_sel_bg.getShowPath();
			var infoParams = {
				bgPath:{
					path:bgPath,
					qnPath:bgPath
				},
				avatar:this.userInfo.avatar?this.userInfo.avatar:'',
				nickname:this.userInfo.nickname?this.userInfo.nickname:'',
				mobile:this.mobile,
				title:this.title,
				start_time:this.mxDatePicker.start_time,
				end_time:this.mxDatePicker.end_time,
				contentList:this.$refs.add_sel_button.getPreviewContent(),
			};
			if(infoParams.title == '')
			{
				this.$api.msg('名称标题不能为空');return ;
			}
			let setObj = this.setObj;
			let s_type_id = setObj.s_type.arr[setObj.s_type.index].id;
			let weekIndex = setObj.weekdata.index;
			if(s_type_id != 1  && weekIndex.length < 1)
			{
				this.showError('请选择'+this.setObj.weekdata.titleArr[this.setObj.weekdata.titleIndex]);
			}
			if(s_type_id == 1 )
			{
				infoParams.s_type_id_text = '每天';
			}else{
				var weekArr = [];
				for(var i=1;i<7;i++)
				{
					if(weekIndex.indexOf(i.toString()) != -1)
					{
						weekArr.push(this.setObj.weekdata.arr[(i-1)].title)
					}
				}
				if(weekIndex.indexOf('0') != -1)
				{
					weekArr.push('星期天')
				}
				infoParams.s_type_id_text = weekArr.join('、');
			}
			infoParams.can_bk = this.canBk;
			infoParams.dk_start_time = this.mxDatePicker.dk_start_time;
			infoParams.dk_end_time = this.mxDatePicker.dk_end_time;
			var dataList = this.$refs.show_form.getItemList();
			if(dataList.length > 0)
			{
				var opeList = this.$api.opeDiyFormData(dataList);
				if(opeList.length < 1)
				{
					return;
				}
			}else{
				var opeList = [];
			}
			infoParams.opeList = opeList;
			//console.log(infoParams)
			uni.setStorageSync('form-daka-detail-preview-data',infoParams);
			this.$api.navigateTo('/pages/form/daka/detail-preview'); 
		},
		showSelForm(){
			this.$refs.sel_form.tggleStatus(true);
		},
		selFormData:function(info){
			if(info)
			{
				this.$refs.show_form.changeItemlist();
				this.formData.push(info);
			}
		},
		selFormDataChange(data){
			this.formData = data;
		},
		inputMobile(){
			var that = this;
			uni.showModal({
				title:'输入手机号',
				content:that.mobile,
				editable:true,
				placeholderText:'',
				success(res){
					if(res.confirm)
					{
						var checkMobile = that.$api.isPhoneNumber(res.content);
						if(checkMobile == false)
						{
							that.mobile = '';
							that.$api.msg('请输入正确的手机号');
						}else{
							that.mobile = res.content;
						}
					}
				}
			})
		},
		async submit(){
			var that = this;
			let postParams = {};
			//判断是否登录
			//判断是否登录
			var checkLogin = that.$api.checkLogin(false);
			if(checkLogin == false)
			{
				that.loginCallBackType = 'submit';
				that.$refs.wechat_login.saveShowStatus(true);
				return ;
			}
			//判断标题
			postParams.title = that.title?that.title.trim():'';
			if(postParams.title == '')
			{
				that.showError('名称标题不能为空');
			}
			//判断电话号
			postParams.mobile = that.mobile;
			//判断时间
			postParams.start_time = that.$api.timeToNumber(that.mxDatePicker.start_time);
			postParams.end_time = that.$api.timeToNumber(that.mxDatePicker.end_time);
			if(postParams.end_time && postParams.end_time < that.$api.getTime())
			{
				that.showError('结束时间不能小于当前时间');
			}else if(postParams.start_time && postParams.end_time && postParams.start_time > postParams.end_time)
			{
				that.showError('开始时间不能大于结束时间');
			}else if(!postParams.start_time && postParams.end_time)
			{
				that.showError('请选择开始时间');
			}else if(postParams.start_time && !postParams.end_time)
			{
				that.showError('请选择结束时间');
			}
			let setObj = this.setObj;
			let s_type_id = setObj.s_type.arr[setObj.s_type.index].id;
			let weekIndex = setObj.weekdata.index;
			if(s_type_id != 1  && weekIndex.length < 1)
			{
				that.showError('请选择'+that.setObj.weekdata.titleArr[that.setObj.weekdata.titleIndex]);
			}else if(s_type_id == 1 )
			{
				if(that.txStatus == 1 && weekIndex.length < 1)
				{
					that.showError('请选择'+that.setObj.weekdata.titleArr[that.setObj.weekdata.titleIndex]);
				}
			}
			postParams.s_type_id = s_type_id;
			postParams.weeklist = weekIndex;
			postParams.txStatus = that.txStatus;
			postParams.tx_time = that.mxDatePicker.tx_time;
			//判断可打卡时间范围
			let dk_start_time = that.mxDatePicker.dk_start_time;
			let dk_end_time = that.mxDatePicker.dk_end_time;
			let dk_start_time_arr = dk_start_time.split(':');
			let dk_end_time_arr = dk_end_time.split(':');
			let dk_start_time_arr0 = parseInt(dk_start_time_arr[0]);
			let dk_start_time_arr1 = parseInt(dk_start_time_arr[1]);
			let dk_end_time_arr0 = parseInt(dk_end_time_arr[0]);
			let dk_end_time_arr1 = parseInt(dk_end_time_arr[1]);
			if((dk_start_time_arr0 > dk_end_time_arr0) || ((dk_start_time_arr0 == dk_start_time_arr0) && (dk_start_time_arr1 >= dk_end_time_arr1)) )	
			{
				that.showError('打卡时间段开始时间不能早于或等于结束时间');
			}
			postParams.dk_start_time = dk_start_time;
			postParams.dk_end_time = dk_end_time;
			//其他设置
			postParams.canBk = that.canBk;
			postParams.inputLast = that.inputLast;
			
			//判断推送圈子
			postParams.pushCircleStatus = that.pushCircleStatus;
			if(postParams.pushCircleStatus == 1)
			{
				if(that.pushCircleList.length <1)
				{
					postParams.pushCircleId = 0;
				}else{
					postParams.pushCircleId = that.pushCircleList[that.push_circle_index].circle_id;
				}
			}else{
				postParams.pushCircleId = 0;
			}
			//判断权限
			if(that.joinRuleList.length > 0)
			{
				postParams.show_rule_status = that.joinRuleList[that.joinRuleListIndex].id;
			}else{
				postParams.show_rule_status = 0;
			}
			
			//判断是否可以重复参加
			postParams.againMore = that.againMore;
			var text = (that.isCopy == true)?'确认复制本条数据':'确认修改';
			that.$api.showModal(text,'',async function(res){
				if(res !='yes')
				{
					return false;
				}
				var text = '';var c_text = '';
				//判断需要填写的表单内容
				var dataList = that.$refs.show_form.getItemList();
				if(dataList.length > 0)
				{
					var opeList = that.$api.opeDiyFormData(dataList);
					if(opeList.length < 1)
					{
						return false;
					}
					 text = that.$refs.show_form.getItemListText();
				}
				postParams.dataList = dataList;
				//判断上传的表单内容
				var checkLength = that.$refs.add_sel_button.checkFormData();
				if(checkLength == -1)
				{
					//返回-1代表有错误发生了
					return false; 
				}
				if(checkLength > 0)
				{
					c_text = that.$refs.add_sel_button.getFormDataText();
				}
				var need_check_text = postParams.title +','+ c_text +','+ text;
				if(need_check_text)
				{
					var res = await that.$api.checkQiniuText(need_check_text);
					postParams.checkRes = res;
					if(!res.status)
					{
						return ;
					}
				}
				if(checkLength > 0)
				{
					//大于0表示，需要验证上传等
					var contentList = await that.$refs.add_sel_button.getContent();
					if(contentList.length < 1)
					{
						that.showError();
					}
					postParams.contentList = contentList;
				}else{
					postParams.contentList = [];
				}
				//获取背景图
				var bgimg = await that.$refs.add_sel_bg.getBgUrl();
				if(!bgimg)
				{
					return false;
				}
				postParams.bg_image = bgimg.qnPath?bgimg.qnPath:'';
				postParams.s_id = that.s_id;
				that.$api.loadingMsg('');
				if(that.isCopy == true)
				{
					var url = 'statistics/create';
					postParams.type = 2;
					postParams.s_id = 0;
				}else{
					var url = 'statistics/edit';
				}
				that.$http.post(url,postParams).then(res=>{
					that.$api.msg(res.msg);
					if(res.code == 1)
					{
						setTimeout(function(){
							uni.redirectTo({
								url:'/pages/form/daka/detail?s_id='+res.data.s_id
							})
						},1000)
						
					}
					
				})
			})
			
			//if()
			
			/* var bgimg = await this.$refs.add_sel_bg.getBgUrl();
			console.log(bgimg) */
			//var contentList = await this.$refs.add_sel_button.getContent();
			//console.log('contentList',contentList)
		},
		showError(text)
		{
			if(text)
			{
				this.$api.msg(text);
			}
			throw 'error';
		}
	}
}